import java.util.*;

public class Main {
    // 双色球规则常量（main依赖，必须保留）
    private static final int RED_BALL_COUNT = 6;
    private static final int BLUE_BALL_COUNT = 1;
    private static final int RED_BALL_MIN = 1;
    private static final int RED_BALL_MAX = 33;
    private static final int BLUE_BALL_MIN = 1;
    private static final int BLUE_BALL_MAX = 16;

    // -------------------------- 核心main方法（提取并可运行）--------------------------
    public static void main(String[] args) {
        // 1. 初始化输入扫描器
        Scanner scanner = new Scanner(System.in);
        // 2. 程序运行状态标记（true=持续运行，false=退出）
        boolean running = true;

        // 3. 欢迎信息
        System.out.println("欢迎使用双色球开奖验证程序！");

        // 4. 主循环：显示菜单→接收选择→执行功能
        while (running) {
            showMenu(); // 显示主菜单
            int choice = getIntInput(scanner, "请选择操作: "); // 获取有效整数选择

            // 5. 根据选择执行对应功能
            switch (choice) {
                case 1:
                    generateMultipleTickets(scanner); // 生成5注彩票
                    break;
                case 2:
                    redeemTicket(scanner); // 兑奖功能
                    break;
                case 3:
                    System.out.println("感谢使用，再见！");
                    running = false; // 退出循环，结束程序
                    break;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
        scanner.close(); // 关闭扫描器，释放资源
    }
    // -------------------------- main依赖的所有方法（必须配套保留）--------------------------

    /**
     * 显示主菜单（main调用）
     */
    private static void showMenu() {
        System.out.println("\n========== 双色球彩票系统 ==========");
        System.out.println("1. 生成5注彩票号码");
        System.out.println("2. 兑奖");
        System.out.println("3. 退出");
        System.out.println("==================================");
    }

    /**
     * 生成5注彩票号码（main→case1调用）
     */
    private static void generateMultipleTickets(Scanner scanner) {
        System.out.println("\n生成5注彩票号码:");
        for (int i = 0; i < 5; i++) {
            int[] redBalls = generateRedBalls();
            int blueBall = generateBlueBall();
            System.out.println("第" + (i + 1) + "注: " + formatBalls(redBalls, blueBall));
        }
        System.out.println("按回车键返回主菜单...");
        scanner.nextLine(); // 等待用户按回车
    }

    /**
     * 兑奖功能（main→case2调用）
     */
    private static void redeemTicket(Scanner scanner) {
        // 清除缓冲区换行符
        if (scanner.hasNextLine()) {
            scanner.nextLine();
        }

        System.out.println("\n兑奖功能");
        System.out.println("请输入您的投注号码（格式：03 12 18 22 27 33 09，前6个为红球，最后1个为蓝球）：");

        String input = scanner.nextLine().trim();
        String[] numbers = input.split("\\s+");

        if (numbers.length != 7) {
            System.out.println("输入格式错误！请按照示例格式输入7个数字。");
            return;
        }

        try {
            int[] userRedBalls = new int[6];
            for (int i = 0; i < 6; i++) {
                userRedBalls[i] = Integer.parseInt(numbers[i]);
                if (userRedBalls[i] < RED_BALL_MIN || userRedBalls[i] > RED_BALL_MAX) {
                    System.out.println("红球号码应在" + RED_BALL_MIN + "-" + RED_BALL_MAX + "之间！");
                    return;
                }
                // 检查红球重复
                for (int j = 0; j < i; j++) {
                    if (userRedBalls[i] == userRedBalls[j]) {
                        System.out.println("红球号码不能重复！");
                        return;
                    }
                }
            }

            int userBlueBall = Integer.parseInt(numbers[6]);
            if (userBlueBall < BLUE_BALL_MIN || userBlueBall > BLUE_BALL_MAX) {
                System.out.println("蓝球号码应在" + BLUE_BALL_MIN + "-" + BLUE_BALL_MAX + "之间！");
                return;
            }

            // 生成开奖号码
            int[] drawRedBalls = generateRedBalls();
            int drawBlueBall = generateBlueBall();

            // 显示号码
            System.out.println("您的投注号码: " + formatBalls(userRedBalls, userBlueBall));
            System.out.println("开奖号码: " + formatBalls(drawRedBalls, drawBlueBall));

            // 判断中奖等级
            int prizeLevel = checkPrizeLevel(userRedBalls, userBlueBall, drawRedBalls, drawBlueBall);

            // 输出中奖结果
            if (prizeLevel == 0) {
                System.out.println("很遗憾，您未中奖。");
            } else {
                String[] prizeNames = {"", "一等奖", "二等奖", "三等奖", "四等奖", "五等奖", "六等奖"};
                System.out.println("恭喜您中了" + prizeNames[prizeLevel] + "！");
            }

        } catch (NumberFormatException e) {
            System.out.println("输入格式错误！请输入数字。");
        }

        System.out.println("按回车键返回主菜单...");
        scanner.nextLine();
    }

    /**
     * 获取整数输入（main调用，处理非数字输入）
     */
    private static int getIntInput(Scanner scanner, String prompt) {
        System.out.print(prompt);
        while (!scanner.hasNextInt()) {
            System.out.print("请输入有效数字: ");
            scanner.next(); // 丢弃非数字输入
        }
        return scanner.nextInt();
    }

    /**
     * 判断中奖等级（兑奖功能调用）
     */
    private static int checkPrizeLevel(int[] userRedBalls, int userBlueBall, int[] drawRedBalls, int drawBlueBall) {
        // 计算红球匹配数
        int redMatchCount = 0;
        for (int userRed : userRedBalls) {
            for (int drawRed : drawRedBalls) {
                if (userRed == drawRed) {
                    redMatchCount++;
                    break;
                }
            }
        }

        // 判断蓝球是否匹配
        boolean blueMatch = (userBlueBall == drawBlueBall);

        // 按规则判断等级
        if (redMatchCount == 6 && blueMatch) return 1;    // 一等奖
        if (redMatchCount == 6 && !blueMatch) return 2;   // 二等奖
        if (redMatchCount == 5 && blueMatch) return 3;    // 三等奖
        if ((redMatchCount == 5 && !blueMatch) || (redMatchCount == 4 && blueMatch)) return 4; // 四等奖
        if ((redMatchCount == 4 && !blueMatch) || (redMatchCount == 3 && blueMatch)) return 5; // 五等奖
        if (blueMatch) return 6;                          // 六等奖（0-2个红球+蓝球）
        return 0; // 未中奖
    }

    /**
     * 生成6个不重复红球（生成号码、兑奖功能调用）
     */
    private static int[] generateRedBalls() {
        Random random = new Random();
        Set<Integer> redBalls = new HashSet<>();

        while (redBalls.size() < RED_BALL_COUNT) {
            int ball = random.nextInt(RED_BALL_MAX - RED_BALL_MIN + 1) + RED_BALL_MIN;
            redBalls.add(ball);
        }

        // 转换为数组并排序
        int[] ballArr = redBalls.stream().mapToInt(Integer::intValue).toArray();
        Arrays.sort(ballArr);
        return ballArr;
    }

    /**
     * 生成1个蓝球（生成号码、兑奖功能调用）
     */
    private static int generateBlueBall() {
        Random random = new Random();
        return random.nextInt(BLUE_BALL_MAX - BLUE_BALL_MIN + 1) + BLUE_BALL_MIN;
    }

    /**
     * 格式化号码显示（生成号码、兑奖功能调用）
     */
    private static String formatBalls(int[] redBalls, int blueBall) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < redBalls.length; i++) {
            if (i > 0) sb.append(" ");
            // 个位数补0
            if (redBalls[i] < 10) sb.append("0");
            sb.append(redBalls[i]);
        }
        sb.append(" | ");
        if (blueBall < 10) sb.append("0");
        sb.append(blueBall);
        return sb.toString();
    }
}